package com.foreveross.chameleonsdk;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

import com.foreveross.chameleonsdk.ChameleonSDK.ChameleonInitCallBack;
import com.foreveross.util.DialogUtils;

/**
 * 复制asset文件www/www.zip并解压www文件、应用验证功能.<br>
 * 初始化后默认会跳转到CAppliation指定的第一个模块界面,如果想自定义跳转请重写{@link# gotoFirstActivity()}
 * 
 * @author Jianfeng.Lao
 * 
 */
public class ChameleonSDKActivity extends Activity implements
		ChameleonInitCallBack {
	/**
	 * 检查www.zip解压情况和App验证
	 */
	final public void initChameleonSDK() {
		ChameleonSDK.getInstance().initChameleonSDK(this, this);
	}

	/**
	 * 跳转第一个模块
	 */
	protected void gotoFirstActivity() {
		ChameleonSDK.getCApplication().startFirstModule();
		finish();
	}

	@Override
	public void onError(Exception err) {

	}

	@Override
	public void onSuccess() {
		gotoFirstActivity();
	}

	/**
	 * 配置可以跳过应用验证
	 * 
	 * @param seekAuth
	 */
	public void setSeekAuth(boolean seekAuth) {
		ChameleonSDK.getInstance().setSeekAuth(seekAuth);
	}

	@Override
	public void onAuthAppError(String code) {
		if (code != null) {
			if (code.equals("400")) {
				Dialog dialog = DialogUtils.getAlertDialog(this, "提示", "应用已删除",
						"确定", null, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
								finish();
							}
						}, null);
				dialog.show();
			} else if (code.equals("403")) {
				Dialog dialog = DialogUtils.getAlertDialog(this, "提示",
						"应用验证失败", "确定", null,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
								finish();
							}
						}, null);
				dialog.show();
			} else if (code.equals("404")) {
				Toast.makeText(this, "应用验证失败，请检查网络", Toast.LENGTH_SHORT).show();
				finish();
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ChameleonSDK.getInstance().cancel();
	}
	/**
	 * 设置后调用initChameleonSDK()会重新解压zip包
	 * @param unZipAgain
	 */
	public void reUnZipWwwFolder(boolean unZipAgain) {
		ChameleonSDK.getInstance().setReUnZipWwwFolder(unZipAgain);
	}

}
